<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Form extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
	public $base_uri = array();
			
	function __construct(){
			parent::__construct();
			$main =   & get_config();
			$this->base_uri['base_url'] = $main['base_url'];
	
	}
	
	public function index()
	{
		
		
				
		/*
		$this->load->model('Basemodel');
		
		$data = array();
		$data['data'] = $this->Basemodel->get(); 
		$data['base_url'] = $this->base_uri;

		echo $this->input->get('name');
		
		$this->load->view('header_view');
		$this->load->view('main_view', $this->base_uri);
		$this->load->view('create_message', $data);
	    */
	}
	
	public function former(){
					
			echo $this->input->get('iduser');
						
			$this->load->model('Formmodel');
			$this->load->model('Usermodel');
			
			$data = array();
			$data['data'] = $this->Formmodel->get();
			$data['user'] = $this->Usermodel->get();
			$data['iduser'] = $this->session->userdata('id_user');
			
			$this->load->view('header_view');
			$this->load->view('main_view', $this->base_uri);
			$this->load->view('former_view', $data); 
	        
	}
	
	public function addform(){
		
			echo $this->input->get('idtur').'<br>';
			echo $this->input->get('type').'<br>';
			echo $this->input->get('pilot').'<br>';
			echo $this->input->get('iduser').'<br>';
			
			
			$data = array();
			$data['iduser'] = $this->input->get('iduser');
			$data['idtur'] = $this->input->get('idtur');
			if($this->input->get('type') == 2){
				$data['pilot'] = $this->input->get('pilot');
			}else{
				$pilot = array();
				$pilot[0] = $this->input->get('cone');
				$pilot[1] = $this->input->get('ctwo');
				$data['pilot'] = serialize($pilot);
				//Вычисляем статус победы одной команды, другой команды или ничьи
				switch((int)$pilot[0] > (int)$pilot[1]){
					case true : $data['common'] = 1; break;
					case false : $data['common'] = 2; break;
				}
				if(((int)$pilot[0] == (int)$pilot[1])) $data['common'] = 0;
			};
			
			
			$this->load->model('Addmodel');
			
			$this->Addmodel->put($data);
			
			$this->load->helper('url');	
	        redirect('/create/');
	        
	        //$this->load->view('header_view');
			//$this->load->view('main_view', $this->base_uri);
	}
	
	public function additogform(){
		
			echo $this->input->get('idtur').'<br>';
			echo $this->input->get('type').'<br>';
			echo $this->input->get('pilot').'<br>';
						
			
			$data = array();
			$data['idtur'] = $this->input->get('idtur');
			if($this->input->get('type') == 2){
				$data['pilot'] = $this->input->get('pilot');
			}else{
				$pilot = array();
				$pilot[0] = $this->input->get('cone');
				$pilot[1] = $this->input->get('ctwo');
				$data['pilot'] = serialize($pilot);
				//Вычисляем статус победы одной команды, другой команды или ничьи
				switch((int)$pilot[0] > (int)$pilot[1]){
					case true : $data['common'] = 1; break;
					case false : $data['common'] = 2; break;
				}
				if(((int)$pilot[0] == (int)$pilot[1])) $data['common'] = 0;
			};
			
			
			$this->load->model('Additogmodel');
			
			$this->Additogmodel->put($data);
			
			$this->load->helper('url');	
	        redirect('/create/');
	        	        
	}
	
	public function checkform(){
		$data['idtur'] = $this->input->get('idtur');
		
		$this->load->model('Checkmodel');
		$this->Checkmodel->put($data);
			
		$this->load->helper('url');	
	    redirect('/create/');
	}
	
	public function mode()
	{
		$this->load->view('header_view');
		$this->load->view('main_view', $this->base_uri);
		$this->load->view('mode_view');
	}
	
	public function creater()
	{
		if($this->input->get('turname')){
			echo $this->input->get('turname');
			$this->load->model('Createmodel');
			echo $this->Createmodel->put();
		}
		
		$this->load->view('header_view');
		$this->load->view('main_view', $this->base_uri);
		$this->load->view('create_view', $this->base_uri);
	}
	
	public function requester()
	{
		$this->load->model('Createlistmodel');
		$data['data'] = $this->Createlistmodel->getlistone();
		$this->load->view('request_view', $data);
	}
	
	public function createlist()
	{
		if((int)$this->input->get('fcheck') > 1){
			$pilot = Array();
			for($i=0; $i < (int)$this->input->get('fcheck')+1; $i++){
				$pilot[$i] = $this->input->get('pilot'.$i);
			}
		}
				
		if($this->input->get('listname')){
			echo $this->input->get('listname');
			$this->load->model('Createlistmodel');
			echo $this->Createlistmodel->put(serialize($pilot));
		}
						
		$this->load->view('header_view');
		$this->load->view('main_view', $this->base_uri);
		$this->load->view('createlist_view', $this->base_uri);
	}
	
	public function addlists()
	{
		$this->load->model('Createlistmodel');
		
		$data = array();
		$data['data'] = $this->Createlistmodel->getlist();
		$data['base_url'] = $this->base_uri['base_url'];
		$data['id_user'] = $this->session->userdata('id_user');
		
		$this->load->view('header_view');
		$this->load->view('main_view', $this->base_uri);
		$this->load->view('addlist_view', $data);
	}
	
	public function modlist()
	{
		$this->load->model('Createlistmodel');
		$data['data'] = $this->Createlistmodel->getlistone();
		$data['idlist'] = $this->input->get('id');
		
		$this->load->view('header_view');
		$this->load->view('main_view', $this->base_uri);
		$this->load->view('modlist_view', $data);
	}
	
	public function modlistput(){
		
		$this->load->model('Createlistmodel');
				
		if((int)$this->input->get('fcheck') > 1){
			$pilot = Array();
			for($i=0; $i < (int)$this->input->get('fcheck')+1; $i++){
				$pilot[$i] = $this->input->get('pilot'.$i);
			}
		}
		
		$this->Createlistmodel->putlistone(serialize($pilot), $this->input->get('idlist'));
		
		$this->load->helper('url');	
	    redirect('/form/addlists/');
	}
	
	public function droplist(){
	      
	    $this->load->model('Createlistmodel');
	    $this->Createlistmodel->drop($this->input->get('id'));
	    
	    $this->load->helper('url');	
	    redirect('/form/addlists/');
	}
}

